package controller;

import com.google.common.collect.ImmutableMap;
import common.pojo.CommonResult;
import common.utils.JsonUtils;
import httpclient.HttpClientUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.math.NumberUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import pojo.UserShopInfo;
import pojo.form.ChatRecordQueryForm;
import service.ChatRecordService;
import service.cfg.ShopCfg;

/**
 * Created by asus on 2018/11/22.
 */
@Controller
@RequestMapping("/chat/record")
public class ChatRecordController {

    @Autowired
    ChatRecordService chatRecordService;
    @Autowired
    ShopCfg shopCfg;

    /**
     * 查阅历史消息，一条
     *
     * @param customId
     * @param shopId
     * @return
     */
    @RequestMapping(value = "/detail", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult getChatRecord(
            @RequestParam(value = "customId") String customId,
            @RequestParam(value = "shopId") String shopId
    ) {
        if (StringUtils.isBlank(customId)) {
            return CommonResult.returnFailsWrapper("请传入customId");
        }
        if (StringUtils.isBlank(shopId)) {
            return CommonResult.returnFailsWrapper("请传入shopId");
        }
        return CommonResult.returnDataWrapper(chatRecordService.getChatRecord(NumberUtils.toLong(customId), NumberUtils.toLong(shopId)));
    }


    @RequestMapping(value = "/msg", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult getChatRecord(
            @RequestParam(value = "shopId") String shopId
    ) {
        if (StringUtils.isBlank(shopId)) {
            return CommonResult.returnFailsWrapper("请传入shopId");
        }
        return CommonResult.returnDataWrapper(chatRecordService.getUnRradMsg(NumberUtils.toLong(shopId)));
    }


    @ResponseBody
    @RequestMapping(value = "/page", method = RequestMethod.GET)
    public CommonResult getChatRecordPage(
            @ModelAttribute ChatRecordQueryForm queryForm,
            @RequestParam(value = "start", required = false) String start,
            @RequestParam(value = "limit", required = false) String limit,
            @RequestParam(value = "curPage", required = false, defaultValue = "1") String curPage,
            @RequestParam(value = "pageSize", required = false, defaultValue = "20") String pageSize
    ) {
        if (start != null) {
            curPage = String.valueOf(NumberUtils.toInt(start, 0) / NumberUtils.toInt(limit, 10) + 1);
            pageSize = limit;
        }
        return CommonResult.returnDataWrapper(chatRecordService.getChatRecord(queryForm, NumberUtils.toInt(curPage), NumberUtils.toInt(pageSize)));
    }

    @ResponseBody
    @RequestMapping(value = "/pages", method = RequestMethod.GET)
    public CommonResult getChatRecordPages(
            @RequestParam(value = "wxId", required = false) String wxId,
            @RequestParam(value = "customId", required = false) String customId,
            @RequestParam(value = "start", required = false) String start,
            @RequestParam(value = "limit", required = false) String limit,
            @RequestParam(value = "curPage", required = false, defaultValue = "1") String curPage,
            @RequestParam(value = "pageSize", required = false, defaultValue = "20") String pageSize
    ) {
        if (start != null) {
            curPage = String.valueOf(NumberUtils.toInt(start, 0) / NumberUtils.toInt(limit, 10) + 1);
            pageSize = limit;
        }
        if (StringUtils.isBlank(wxId)) {
            return CommonResult.returnFailsWrapper("请传入wxId");
        }
        CommonResult commonResult = HttpClientUtils.get(shopCfg.getShopInfo(), ImmutableMap.of("wxId", wxId));
        UserShopInfo shopInfo = JsonUtils.objectToPojo(commonResult.getData(), UserShopInfo.class);
        if (shopInfo == null) {
            return CommonResult.returnFailsWrapper("此店主信息不存在");
        }
        ChatRecordQueryForm  queryForm = new ChatRecordQueryForm();
        queryForm.setShopId(shopInfo.getId());
        if(StringUtils.isNotBlank(customId)){
            queryForm.setCustomId(NumberUtils.toLong(customId));
        }

        return CommonResult.returnDataWrapper(chatRecordService.getChatRecords(queryForm, NumberUtils.toInt(curPage), NumberUtils.toInt(pageSize)));
    }
}
